package chucnang;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.Timer;

import phantrang.PT_MuaHang;

import Table.TDanhSachBanHang;
import Table.TDanhSachNhapHang;
import form.DSJ_FormMuaHang;
import trangchu.TrangChu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Vector;

public class TongHop extends JPanel implements ActionListener {

	private JPanel plNorth,plCenter,plSouth;
	public JButton btndsmuahang;
	public JButton btndsbanhang;
	private TDanhSachNhapHang tabledsnhaphang = new TDanhSachNhapHang(this);
	private TDanhSachBanHang tabledsbanhang = new TDanhSachBanHang();
	private Integer soTrang = 1; 
	private Integer sTT = 0;
	public JComboBox cmbtrang;
	public JButton btnBack,btnNext;
	private TDanhSachNhapHang dsnhaphang; 
	
	public TongHop(TrangChu trangchu) {
		this.setLayout(new BorderLayout(10,10));
		plNorth = new JPanel();
		plNorth.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(plNorth,BorderLayout.NORTH);
		this.btndsmuahang = new JButton("Danh Sách Nhập Hàng",new ImageIcon("images/danhsachnhaphang.png"));
		this.btndsmuahang.setVerticalTextPosition(JButton.BOTTOM);
		this.btndsmuahang.setHorizontalTextPosition(JButton.CENTER);		
		plNorth.add(this.btndsmuahang);
		
		this.btndsbanhang = new JButton("Danh Sách Bán Hàng",new ImageIcon("images/danhsachbanhang.png"));
		this.btndsbanhang.setVerticalTextPosition(JButton.BOTTOM);
		this.btndsbanhang.setHorizontalTextPosition(JButton.CENTER);		
		plNorth.add(this.btndsbanhang);
		
		
		plCenter = new JPanel();
		plCenter.setLayout(new BorderLayout());
		this.add(plCenter,BorderLayout.CENTER);
		plCenter.add(tabledsnhaphang,BorderLayout.CENTER);
		
		plSouth = new JPanel();
		plSouth.setLayout(new BorderLayout());
		add(plSouth,BorderLayout.SOUTH);
		JPanel plphantrang = new JPanel();
		plSouth.add(plphantrang,BorderLayout.CENTER);
		btnBack = new JButton("Trước");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cmbtrang.setSelectedItem(soTrang-1);
			}
		});
		plphantrang.add(btnBack);
		
		cmbtrang = new JComboBox();
		cmbtrang.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println(cmbtrang.getSelectedItem());
				soTrang = (Integer) cmbtrang.getSelectedItem();
			
				loaddata();
				cmbtrang.setSelectedItem(soTrang);
				
			}
		});
		plphantrang.add(cmbtrang);
	
		btnNext = new JButton("Sau");	
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cmbtrang.setSelectedItem(soTrang+1);
			}
		});
		plphantrang.add(btnNext);
		
		
		JPanel plSouth2 = new JPanel();
		plSouth.add(plSouth2,BorderLayout.SOUTH);		
		plSouth2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JLabel lblgoithieu = new JLabel("Phần mềm quản lý bán hàng ShopM                ");
		lblgoithieu.setFont(new Font("Tahoma", 0, 10));
		lblgoithieu.setFont(new Font("Tahoma", 3, 11));		
		lblgoithieu.setForeground(Color.blue);
		plSouth2.add(lblgoithieu);
		final JLabel lblgioserver = new JLabel();
		plSouth2.add(lblgioserver);
		 final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a | dd/MM/yyyy");
	        Timer time = new Timer(1000, new ActionListener()
	        {
	            public void actionPerformed(ActionEvent e)
	            {
	                Date d = new Date(System.currentTimeMillis());
	                lblgioserver.setText(sdf.format(d.getTime()));
	            }
	        });
	        time.start();
		//skien
		this.btndsbanhang.addActionListener(this);
		this.btndsmuahang.addActionListener(this);
	}
	// phan trang
		public void loaddata() {
			try {
			PT_MuaHang ptMuaHang = new PT_MuaHang();
			Integer soLuongR;
			Vector<DSJ_FormMuaHang> danhSach;
			soLuongR = ptMuaHang.getsoDoiTac();
			danhSach = ptMuaHang.getListDoiTac(soTrang);
			
			if ((soLuongR % 22) == 0 ) {
				Combo((soLuongR / 22));
			}else Combo((soLuongR / 22)+1);
			
			tabledsnhaphang.model.getDataVector().removeAllElements();
			tabledsnhaphang.model.fireTableDataChanged();
			
			
			sTT = (soTrang-1)*22+1;
			for (int i = 0; i < danhSach.size(); i++) {
				Object[] oPerson = {
						danhSach.get(i).getMaSP().toString(),
						danhSach.get(i).getTenSP().toString(),
						danhSach.get(i).getKho().toString(),
						danhSach.get(i).getDonViSP().toString(),
						danhSach.get(i).getGiaVon().toString(),
						danhSach.get(i).getSoLuong().toString(),
						danhSach.get(i).getThanhTien().toString(),
						danhSach.get(i).getNhapHang_ID().toString()};
				//sTT++;
				tabledsnhaphang.model.insertRow(i, oPerson);
			}
			tabledsnhaphang.model.fireTableDataChanged();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public void Combo(int numItems)
		{
			Vector v = new Vector(numItems);
			for (int i = 1; i <= numItems; i++) {
				v.add(new Integer(i));
			}
			DefaultComboBoxModel model = new DefaultComboBoxModel(v);
			cmbtrang.setModel(model);
		}
		
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btndsbanhang){
			
			plCenter = new JPanel();
			plCenter.setLayout(new BorderLayout());
			this.add(plCenter,BorderLayout.CENTER);			plCenter.add(tabledsbanhang,BorderLayout.CENTER);
		}
		if(e.getSource() == btndsmuahang){
			loaddata();
			plCenter = new JPanel();
			plCenter.setLayout(new BorderLayout());
			this.add(plCenter,BorderLayout.CENTER);
			plCenter.add(tabledsnhaphang,BorderLayout.CENTER);
		}
		
	}

}
